package com.qububu.play.dineTogether.control;

import static com.qububu.utils.ParamConverter.assertNotEmpty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qububu.common.user.dto.UserInfoDto;
import com.qububu.play.dineTogether.dto.DineSummarizeDto;
import com.qububu.play.dineTogether.service.DineSummarizeService;
import com.qububu.utils.DateUtil;
import com.qububu.utils.SystemConstants;
import com.qububu.utils.SystemUtil;

@Controller
@RequestMapping("/play/dine")
public class DineSummarizeControl {

	@Resource
	private DineSummarizeService dineSummarizeService;
	
	/**
	 * 添加总结的界面
	 * */
	@RequestMapping("/admin/infoAddSummarize")
	public String infoAddSummarize(HttpServletRequest req) {
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		
		req.setAttribute("actionId", actionId);
		return "adminDineSummarize";
	}
	
	/**
	 * 添加总结
	 * */
	@RequestMapping("/admin/addSummarize")
	public String addSummarize(HttpServletRequest req) {
		DineSummarizeDto dineSummarize = makeDineSummarizeEntity(req);
		// 存入数据库
		if (dineSummarizeService.doAddSummarize(dineSummarize)) {
			return "redirect:/play/main";
		}
		
		return "redirect:/play/dine/admin/infoAddSummarize";
	}
	
	/**
	 * 根据id查看一次活动的照片等总结
	 * */
	@RequestMapping("/getSummarize")
	public String getSummarizeById(HttpServletRequest req) {
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		DineSummarizeDto dineSummarize = dineSummarizeService.getSummarizeByActionId(actionId);
		req.setAttribute("dineSummarize", dineSummarize);
		
		return "showActionPic";
	}
	
	/**
	 * 查询过期活动的照片等总结
	 * */
	@RequestMapping("/getSummarizes")
	@ResponseBody
	public Map<String, Object> getSummarizes() {
		
		List<DineSummarizeDto> dineSummarizeList = dineSummarizeService.getDineSummarizes();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		resultMap.put("summarizes", dineSummarizeList);
		
		return resultMap;
	}
	
	/**
	 * 修改总结, 根据actionId
	 * */
	@RequestMapping("admin/modifyDineSummarize")
	public String modifyDineSummarize(HttpServletRequest req) {
		DineSummarizeDto dineSummarize = makeDineSummarizeEntity(req);
		if (dineSummarizeService.doModifyDineSummarize(dineSummarize)) {
			return "redirect:/play/main";
		}
		
		return "redirect:/play/dine/admin/infoModiffySummarize";
	}
	
	/**
	 * 根据id删除一个总结
	 */
	@RequestMapping("admin/deleteDineSummarize")
	public Map<String, Object> deleteDineSummarize(HttpServletRequest req) {
		
		String summarizeId = assertNotEmpty(req.getParameter("summarizeId"));
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (dineSummarizeService.doDeleteSummarize(summarizeId)) {
			resultMap.put("result", SystemConstants.AJAX_SUCCESS);
		} else {
			resultMap.put("result", SystemConstants.AJAX_FAILED_ALERT);
			resultMap.put("message", "缓存操作失败,请稍后重试");
		}
		
		return resultMap;
	} 
	
	private DineSummarizeDto makeDineSummarizeEntity(HttpServletRequest req) {
		
		String summarizeId = SystemUtil.getUUIDString();
		String content = SystemUtil.removeAllScriptFromHtml(assertNotEmpty(req.getParameter("content")));
		String actionId = assertNotEmpty(req.getParameter("actionId"));
		String pictures = makePictures(content);
		int createDt = DateUtil.convertCurrentDTTMtoInt();
		String authorId = ((UserInfoDto) req.getSession().getAttribute(SystemConstants.LOGINUSER_SESSION_NAME)).getUserId();
		
		DineSummarizeDto dineSummarize = new DineSummarizeDto(summarizeId, actionId, authorId);
		dineSummarize.setContent(content);
		dineSummarize.setCreateDt(createDt);
		dineSummarize.setPictures(pictures);
		
		return dineSummarize;
	}
	
	/**
	 * 将一系列图片地址用;隔开拼装成一个字符串
	 * */
	private String makePictures(String htmlStr) {
		StringBuilder pictureBuilder = new StringBuilder(); 
		List<String> pictureList = SystemUtil.getImgTagListFromHtml(htmlStr);
		for (String picUrl: pictureList) {
			pictureBuilder.append(picUrl);
			pictureBuilder.append(";");
		}
		pictureBuilder.substring(0, pictureBuilder.length());
		
		return pictureBuilder.toString();
	}
}
